package org.deltaset.desktop;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author dmakariev
 */
public class MenuGroup implements Serializable, Permission {

    private final String title;
    private final String templateSrc;
    private final List<MenuItem> menuItems;
    private final String permission;

    public MenuGroup(MenuGroup mg, PermissionFilter filter) {
        this.title = mg.title;
        this.templateSrc = mg.templateSrc;
        this.menuItems = PermissionUtil.filterMenuItem(mg.menuItems, filter);
        this.permission = mg.permission;
    }

    public MenuGroup(String title, String permission) {
        this(title, null, permission);
    }

    public MenuGroup(String title, String templateSrc, String permission) {
        this.title = title;
        this.templateSrc = templateSrc;
        this.menuItems = new ArrayList<MenuItem>();
        this.permission = permission;
    }

    public String getTitle() {
        return title;
    }

    public String getTemplateSrc() {
        return templateSrc;
    }

    public List<MenuItem> getMenuItems() {
        return menuItems;
    }

    @Override
    public String getPermission() {
        return permission;
    }

    public MenuGroup addRefreshable(final String title, final String src, final String permission, final String... parameters) {
        return add(true, title, src, permission, parameters);
    }

    public MenuGroup add(final Class entityClass, final String src) {
        return addRefreshable(entityClass.getSimpleName(), src, "entityClassName:" + entityClass.getName());
    }

    public MenuGroup add(final MenuItem menuItem) {
        this.menuItems.add(menuItem);
        return this;
    }

    public MenuGroup add(final String title, final String src, final String permission, final String... parameters) {
        return add(false, title, src,permission, parameters);
    }

    private MenuGroup add(final boolean refreshFlag, final String title, final String src, final String permission, final String... parameters) {

        final Map<String, String> parameterMap = new HashMap<String, String>();
        if (parameters.length > 0) {

            for (String string : parameters) {
                if (string.indexOf(":") < 0) {
                    throw new RuntimeException("ERROR: Incorect MenuItem parameter. Missing delimeter ':'. The correct format is 'name:value' but found '" + string + "'");
                }
                final String[] nameValue = string.split(":");
                if (nameValue.length > 2) {
                    throw new RuntimeException("ERROR: Incorect MenuItem parameter. More then one delimeter ':'. The correct format is 'name:value' but found '" + string + "'");
                }
                parameterMap.put(nameValue[0], nameValue[1]);
            }
        }
        this.menuItems.add(new MenuItem(refreshFlag, title, src, permission, parameterMap));
        return this;
    }
}
